package com.xy.mainp.huaban.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.xy.AnimUtils;
import com.xy.img.GlideUtil;
import com.xy.mainp.R;
import com.xy.mainp.base.TittleBarActivity;
import com.xy.mainp.databinding.ViewImgDetailVpBinding;
import com.xy.mainp.model.huaban.ImgEntity;

/**
 * Created by John on 2016/11/22.
 */

public class VpImgDetailView extends FrameLayout {

    private ImgEntity data;
    private ViewImgDetailVpBinding binding;
    private boolean isVis;
    private TittleBarActivity activity;

    public VpImgDetailView(Context context) {
        this(context,null);
    }

    public VpImgDetailView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public VpImgDetailView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_img_detail_vp, this, true);
        if (context instanceof TittleBarActivity){
            activity = (TittleBarActivity) context;
        }
        isVis = true;
        binding.smImgImgDetail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isVis){
                    hideText();
                }else {
                    showText();
                }
            }
        });
        binding.smImgImgDetail.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //获取震动服务
                Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(60);

                Drawable drawable = binding.smImgImgDetail.getDrawable();
                SaveImgPop saveImgPop = new SaveImgPop(getContext(), drawable);
                saveImgPop.showAtLocation(getRootView(), Gravity.BOTTOM,0,0);
                return false;  //false 相应点击事件 true 不处理点击事件
            }
        });
    }

    private void hideText(){
        AnimUtils.sildeOutToBottom(binding.tvImgDetail,250);
        isVis = false;
    }
    private void showText(){
        AnimUtils.sildeInToBottom(binding.tvImgDetail,250);
        isVis = true;
    }
    public void setData(ImgEntity data) {
        this.data = data;
        binding.progressbarImgDetail.setVisibility(VISIBLE);
        binding.tvImgDetail.setText(data.getDesc());
        GlideUtil.loadImage(data.getImgUrl(), binding.smImgImgDetail, false, 0, new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                if (binding.progressbarImgDetail.getVisibility() == View.VISIBLE){
                    binding.progressbarImgDetail.setVisibility(GONE);
                    showText();
                }
                return false;
            }
        });
    }
}
